package com.niit.lcy.areaselref;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author: Chase
 * @Date: 2024/12/10
 */

public class AreaSelRefReducer extends Reducer<Text, Text, Text, Text> {
    private Text outv = new Text();
    private List<String> list = new ArrayList<>();
    private Double maxTotalPrice = Double.MIN_VALUE;
    private Double mixTotalPrice = Double.MAX_VALUE;
    private int total;// 房总数

    /**
     * reduce阶段的核心业务逻辑（统计同一区间面积的最大最小价格和总数，组成然后输出）
     *
     * @param key     面积区间, eg: 大于190㎡...
     * @param values  总价, eg : 95
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        for (Text value : values) {
            double totalPrice = Double.parseDouble(value.toString());
            maxTotalPrice = Math.max(maxTotalPrice, totalPrice);
            mixTotalPrice = Math.min(mixTotalPrice, totalPrice);
            total++;
        }
        list.add(mixTotalPrice + "~" + maxTotalPrice + "w");
        list.add(total + "套房");
        outv.set(list.toString());
        context.write(key, outv);

        total = 0;
        mixTotalPrice = Double.MAX_VALUE;
        maxTotalPrice = Double.MIN_VALUE;
        list.clear();
    }
}
